/**
 * Created by csy on 17/4/22.
 */
var express = require('express');
var crypto = require('crypto');
var formidable = require('formidable');
var util = require('util');
var fs = require('fs');
var router = express.Router();
var msg = require('../../common/msg');
var mkdirs = require('../../model/mkdir');

router.post("/upload", function (req, res, next) {
    var form = new formidable.IncomingForm();
    form.parse(req, function (err, fields, files) {
        let type = files.file.type;
        if (!(type == 'image/jpeg' || type == 'image/png')) {
            res.send(msg(1, "只能上传jpg或者png格式", false));
            return
        }
        let tmp         = files.file.name.split('.');
        let file_type   = String(tmp[tmp.length - 1]).toLocaleLowerCase();
        let name        = tmp[0] + Date.parse(new Date()) + "." + file_type;
        let file        = process.cwd() + "/public/upload/" + name;
        fs.renameSync(files.file.path, file);
        res.send(msg(0, "上传成功", true, { url: "/public/upload/" + name }));

        
    })

})

module.exports = router;